let nameInput = $("#username");
let pwdInput = $("#pwd");
let checkPwdInput = $("#checkPwd");
let telInput = $("#tel");

function formCheck() {
  //取消submit的默认效果。
  let e = window.event;
  e.preventDefault();
  //正则 规定输入内容的格式
  let userPatt = /^[A-z]\w{2,14}$/;
  let pwdPatt = /^.{3,15}$/;
  let checkpwdPatt = /^.{6,15}$/;
  let telPatt = /^1[356789]\d{9}$/;
  let user = nameInput.val();
  let pwd = pwdInput.val();
  let checkPwd = checkPwdInput.val();
  let tel = telInput.val();

  //填入内容都正确才可以提交
  if (userPatt.test(user) && pwdPatt.test(pwd)) { //&& telPatt.test(tel)
    document.frm.submit();
  } else {
    alert("信息校验不通过，请重新填写")
  }
}

//添加失焦事件
nameInput.blur(function () {
  let userPatt = /^[A-z]\w{2,14}$/;
  let user = nameInput.val();
  let msgInput = $("#userMsg");
  if (userPatt.test(user)) {
    $.ajax({
      url: "registerCheck",
      data: {
        account: user
      },
      success: function (data) {
        if (data === "true") {
          msgInput.html("<span style='color:green'>✔️</span>");
        } else {
          msgInput.html("<span style='color:red;margin-left: -60px;'>❌校验不通过</span>");
        }
      }
    });
  } else {
    $("#userMsg").html("<span style='color:red;margin-left: -60px;'>❌校验不通过</span>");
  }
});
pwdInput.blur(function () {
  let pwdPatt = /^.{3,15}$/;
  let pwd = pwdInput.val();
  let msgInput = $("#pwdMsg");
  if (pwdPatt.test(pwd)) {
    msgInput.html("<span style='color:green'>✔️</span>");
  } else {
    msgInput.html("<span style='color:red;margin-left: -60px;'>❌校验不通过</span>");
  }
});
checkPwdInput.blur(function () {
  let pwd = pwdInput.val();
  let checkPwd = checkPwdInput.val();
  let msgInput = $("#checkPwdMsg");
  if (pwd === checkPwd) {
    if (pwd === "") {
      msgInput.html("<span style='color:red;margin-left: -60px;'>❌请输入密码</span>");
    } else {
      msgInput.html("<span style='color:green'>✔️</span>");
    }
  } else {
    msgInput.html("<span style='color:red;margin-left: -60px;'>❌校验不通过</span>");
  }
});
telInput.blur(function () {
  let telPatt = /^1[356789]\d{9}$/
  let tel = telInput.val();
  let msgInput = $("#telMsg");
  if (telPatt.test(tel)) {
    msgInput.html("<span style='color:green'>✔️</span>");
  } else {
    msgInput.html("<span style='color:red;margin-left: -60px;'>❌校验不通过</span>");
  }
});
